package activitytest.example.com.application;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity {
    private  int mLocalVersionCode;
    private RelativeLayout  rl_root;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        initData();

        /**
        try {
            Thread.sleep(3000);
            enterHome();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }**/

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                enterHome();
            }
        },3000);

        initAnimation();
}

    private void initAnimation() {
       AlphaAnimation alphaAnimation= new AlphaAnimation(0,1);
       alphaAnimation.setDuration(2000);
       rl_root.startAnimation((Animation) alphaAnimation);
    }

    private void initData() {
       rl_root =(RelativeLayout) findViewById(R.id.rl_root);
        TextView tv_version_name = (TextView) findViewById(R.id.tv_version_name);
        tv_version_name.setText(getVersionName());

        mLocalVersionCode = getVersionCode();
    }

    private void enterHome() {
        Intent intent= new Intent(MainActivity.this,HomeActivity.class);

        startActivity(intent);
        //重置路由
        finish();
    }

    private int getVersionCode() {
        PackageManager pm = getPackageManager();
        try {
            PackageInfo packageInfo= pm.getPackageInfo(getPackageName(), 0);
            return packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return  0;
    }

    private String getVersionName() {
        PackageManager pm = getPackageManager();
        try {
          PackageInfo packageInfo= pm.getPackageInfo(getPackageName(), 0);
           return packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }


}
